package ru.spbsu.electromenu.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.spbsu.electromenu.service.OrderService;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Date: 5/2/11
 * Time: 12:59 AM
 *
 * @author Artem Titov
 */
public class EditOrderServlet extends EditObjectServlet {
    private static final Logger logger = LoggerFactory.getLogger(EditOrderServlet.class);
    private OrderService orderService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        orderService = getBean("orderService");
        if (orderService == null) {
            logger.error("Failed to instantiate necessary services");
            isValid = false;
            throw new IllegalStateException("Failed to instantiate necessary services");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int result = HttpServletResponse.SC_BAD_REQUEST;
        if (request.getParameter("deleteDish") != null) {
            logger.debug("Deleting dish");
            final int orderId = getParameter(request, "orderId", -1);
            final int dishId = getParameter(request, "dishId", -1);
            final int amount = getParameter(request, "amount", -1);
            if (orderId <= 0 || dishId <= 0 || amount <= 0) {
                result = HttpServletResponse.SC_BAD_REQUEST;
            } else {
                if (1 == orderService.deleteOrderedDish(orderId, dishId, amount)) {
                    result = HttpServletResponse.SC_OK;
                } else {
                    result = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
                }
            }
        } else if (request.getParameter("confirm") != null) {
            logger.debug("Confirm order");
            final int orderId = getParameter(request, "orderId", -1);
            final Boolean confirm = getParameter(request, "confirmStatus", null);
            if (orderId <= 0 || confirm == null) {
            } else {
                result = HttpServletResponse.SC_BAD_REQUEST;
                if (1 == orderService.confirm(orderId, confirm)) {
                    result = HttpServletResponse.SC_OK;
                } else {
                    result = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
                }
            }
        } else if (request.getParameter("delete") != null) {
            logger.debug("Deleting order");
            final int orderId = getParameter(request, "orderId", -1);
            if (orderId <= 0) {
                result = HttpServletResponse.SC_BAD_REQUEST;
            } else {
                if (1 == orderService.delete(orderId)) {
                    result = HttpServletResponse.SC_OK;
                } else {
                    result = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
                }
            }
        }
        if (result != HttpServletResponse.SC_OK) {
            response.sendError(result);
        } else {
            response.sendRedirect("showOrder");
        }
    }
}
